const puppeteer = require('puppeteer')

const url = `https://movie.douban.com/tag/%E7%A7%91%E5%B9%BB?start=20&type=R`

const sleep = time => new Promise(resolve => {
        setTimeout(resolve, time)
    })

;(async () => {  //必须前面有;冒号
   const browser = await puppeteer.launch({

        args: ['--no-sandbox'],
        dumpio: false,
       headless: false
    })

    const page = await browser.newPage()
    await page.goto(url, {
        waitUntil: 'networkidle2'
    })

    const result = await page.evaluate(() => {
        var $ = window.$;   //puppeteer可以拿到window.$对象转为jQuery对象，方便使用
        var items = $('.item')   //里面都是在浏览器执行，用前端的var,别用let
        var links = []

        if (items.length >= 1) {
            items.each((index, item) => {
                var it = $(item)
                console.log(it)
                var dyId = it.find('.cover-wp').data('id');
                var title = it.find('.pic').find('img').attr('alt');
                var cover = it.find('.pic').find('img').attr('src').replace('s_ratio','l_ratio');
                var dyRate=it.find('.rate').text()

                links.push({
                    dyId,
                    title,
                    cover,
                    dyRate
                })
            })
        }
        return links
    })
    console.log(result)

    browser.close()
    process.send({result})  //这个js文件挂载到子进程运行，这是给子进程输出结果数据
    process.exit(0)   //这个js模块挂载到子进程运行，这个是退出子进程
})()